package alsTelecom.GUI;


import alsTelecom.Http.Conexiones;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.FieldChangeListener;
import net.rim.device.api.ui.Font;
import net.rim.device.api.ui.FontFamily;
import net.rim.device.api.ui.component.ButtonField;
/**
 * Class to personalize a Field to be a button with an Image
 * on background
 * @author ALS Telecom
 *
 */
public class MyDialogButton extends ButtonField implements FieldChangeListener {
    private int type;
    private Font font;
    //Get all fonts available on the BlackBerry
    private FontFamily fontFamily[] = FontFamily.getFontFamilies();
    protected static ControladorGUI _gui;
    protected static UserInformation _user; 
    protected static CompartirDialog _dialog;
    protected static TwitterManager _twitter;
    /**
     * 
     * @param user
     * @param gui
     */
    public MyDialogButton (UserInformation user,ControladorGUI gui){
		super("Enviar",Field.FIELD_HCENTER);
		type = 1;
		_gui = gui;
		_user = user;

        //Set the current font to the first in the font list.
        font = fontFamily[0].getFont(FontFamily.SCALABLE_FONT, 20);
        Font.setDefaultFont(font);
    }
    /**
     * 
     * @param label
     * @param gui
     * @param dialog
     */
    public MyDialogButton (String label,ControladorGUI gui,CompartirDialog dialog){
		super(label,Field.FIELD_HCENTER);
	    _gui = gui;
	    _dialog = dialog;
	    //Set the current font to the first in the font list.
	    FontFamily fontFam;
		try {
			fontFam = FontFamily.forName("BBAlpha Sans");
			Font font = fontFam.getFont(FontFamily.SCALABLE_FONT, 20).derive(Font.PLAIN);
			super.setFont(font);
		} catch (ClassNotFoundException e) {

		}   
    }
	/**
	 * 
	 * @param twitterManager
	 * @param gui
	 */
	public MyDialogButton(TwitterManager twitterManager, ControladorGUI gui){
		super("Enviar",Field.FIELD_HCENTER);
		type = 2;
	    _gui = gui;
	    _twitter = twitterManager;
	    //Set the current font to the first in the font list.
	    FontFamily fontFam;
		try {
			fontFam = FontFamily.forName("BBAlpha Sans");
			Font font = fontFam.getFont(FontFamily.SCALABLE_FONT, 20).derive(Font.PLAIN);
			super.setFont(font);
		} catch (ClassNotFoundException e) {

		}
   }
	/**
	 * Listen for navigation Click
	 */
	protected boolean navigationClick(int status, int time){
		switch(type){
			case 1:
				WaitScreen callback = new WaitScreen(_gui.getControlador());
				Conexiones.sendInfo(_user.getInfo());				
				Conexiones.getTwitter(callback);
				break;
			case 2:
				WaitScreen callback2 = new WaitScreen(_gui.getControlador());
				TwitterManager.xauth(_twitter.getUsername(),_twitter.getPassword(),_twitter.getMensaje());
				Conexiones.getTwitter(callback2);
				break;
		}
	 return true;
	}

	 public void fieldChanged(Field field, int context)
     {
         //If the ObjectCoiceField has changed
         if (field instanceof MyDialogButton)
         {
             //Get the new font selected in the ObjectChoiceField and set the
             //current font to match what is selected.

        	 FontFamily fontFam;
     		try {
     			fontFam = FontFamily.forName("BBAlpha Sans");
     			Font font = fontFam.getFont(FontFamily.SCALABLE_FONT, 20).derive(Font.PLAIN);
     			super.setFont(font);
     		} catch (ClassNotFoundException e) {

     		}
         }
     }
	
}

